# -*- coding: utf-8 -*-
"""
Created on Sat Oct 10 16:48:05 2020

@author: Da Hui
"""
#也许用得到的代码
'''
WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, '//*[@id="content"]/div/div[1]/div[3]/div[2]/div[1]/div[2]/div/h3/a')))
driver.find_element_by_xpath('//*[@id="content"]/div/div[1]/div[3]/div[2]/div[1]/div[2]/div/h3/a').click()

WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="interest_sectl"]/div/div[2]/div/div[2]/a')))
driver.find_element_by_xpath('//*[@id="interest_sectl"]/div/div[2]/div/div[2]/a').click()'''


import time
import csv
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import importlib,sys
importlib.reload(sys)

resultArr = []
exist =''

def run(url):
    #创建chorme对象
    driver = webdriver.Chrome()
    driver.get(url)
    #log in-------------------------
    time.sleep(20)#扫码登录
    web='https://movie.douban.com/subject/35441524/comments'#短评主页链接，注意地址只到comments
    last=['','?status=N','?status=F']#看过、在看、想看
    for i in range(3):
        try:
            #打开要爬取的网页
            webnet=web+last[i]
            driver.get(webnet)
#            等待页面加载
            flag=WebDriverWait(driver, 20).until(lambda driver : driver.find_element_by_class_name('comment-item'))
            try:
                while flag:
#                    采集数据
                    print_db(driver)
#                    滚动到页面底部
                    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
#                    等待3s
                    time.sleep(3)
#                    点击下一页
                    driver.find_element_by_class_name('next').click()
#                    检查是否还有下一页
                    WebDriverWait(driver, 20).until(lambda driver : driver.find_element_by_class_name('next'))       
            except IndexError:
                pass
        
        finally:
            driver.quit


def print_db(driver):
#    xpath路径
    userxpath='//*[@id="comments"]/div/div[2]/h3/span[2]/a'
    scorexpath='//*[@id="comments"]/div/div[2]/h3/span[2]/span[2]'
    timexpath='//*[@id="comments"]/div/div[2]/h3/span[2]/span[3]'
    seenxpath='//*[@id="comments"]/div/div[2]/h3/span[2]/span[1]'
    commentpath='//*[@id="comments"]/div/div[2]/p/span'
    
    
        
    try: 
        userElem=driver.find_elements_by_xpath(userxpath)
        scoreElem=driver.find_elements_by_xpath(scorexpath)
        timeeElem=driver.find_elements_by_xpath(timexpath)
        seenElem=driver.find_elements_by_xpath(seenxpath)
        conmentElem=driver.find_elements_by_xpath(commentpath)
            
        for i in range(20):
            user=userElem[i].text
            score=scoreElem[i].get_attribute('title')
            timee=timeeElem[i].text
            seen=seenElem[i].text
            comment=conmentElem[i].text
            resultArr.append([user,score,timee,seen,comment])
#        打印数据    
        print2CSV(resultArr)
        
    except IndexError:
        pass
    
    
        

          
def print2CSV(datas):
    file=open('The result.csv','w',newline='',encoding='utf-8-sig')
    writer = csv.writer(file)
    writer.writerow(['username','score','time','seen','comment'])
    for item in datas:
        writer.writerow([item[0], item[1], item[2],item[3],item[4]])




if __name__ == '__main__':
    url="https://www.douban.com/"
    run(url)
    print('end')